﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Admin_AddXz : System.Web.UI.Page
{
    AccessDAO dao = new AccessDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        IsAdmin.Admin();
        if (!Page.IsPostBack)
        {
            int typeid = Convert.ToInt32(Session["xzId"].ToString());
            string sql = "select * from xzInfo where typeid=" + typeid;
            DataSet ds = dao.getDs(sql);
            foreach (DataRow dr in ds.Tables["message"].Rows)
            {
                this.drpType.Items.Add(dr["typeName"].ToString());
            }
            this.DateTimePicker1.Text=DateTime.Now.ToShortDateString();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int typeid = Convert.ToInt32(Session["xzId"].ToString());
        string name = this.txtTitle.Text;
        string dx = this.txtDx.Text.ToString();
        string language = this.txtLanguage.Text;
        string hj = this.txtHj.Text;
        string file = this.FileUpload1.PostedFile.FileName;

        string fileName = "File/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();


        DateTime tt = Convert.ToDateTime(this.DateTimePicker1.Text.ToString());
        string time = tt.ToShortDateString();

        string content = this.FCKeditor1.Value;
        if (!name.Equals("") && !dx.Equals("") && !language.Equals("") && !hj.Equals("") && !file.Equals(""))
        {
            if (!Path.GetExtension(file).Equals(".txt"))
            {
                fileName += Path.GetExtension(file);
                string ss = "insert into xzMessage(tyId,name,xzlanguage,xzdx,xzhj,xzFile,xzTime,content) values(" + typeid + ",'" + name + "','" + language + "','" + dx + "','" + hj + "','" + fileName + "','" + time + "','" + content + "')";
                if (dao.getComm(ss) != 0)
                {
                    this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Association/") + fileName);
                    Server.Transfer("xzMessage1.aspx");
                }
                else
                {
                    RegisterStartupScript("check", "<script language='javascript'>alert('添加失败');</script>");
                }
            }
            else
            {
                RegisterStartupScript("check", "<script language='javascript'>alert('不支持.txt文件');</script>");
            }
        }
        else
        {
            RegisterStartupScript("check", "<script language='javascript'>alert('填写完整');</script>");
        }
    }
    //取消
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("xzMessage1.aspx");
    }
}

